﻿@{ ViewBag.Title = "Index";
    Layout = "~/Areas/Shared/_LayuiList.cshtml"; }

<input style="display: none" id="tbid" value="@ViewBag.tbid">
<blockquote class="layui-elem-quote layui-text">
    可对主表的AfterAdd、AfterEdit、AfterFinish、AfterDelete，子表的AfterGridAdd、AfterGridEdit、AfterGridDel事件进行自定义，以上事件分别在添加完成、编辑完成、删除完成后被调用<br>
    通过自定义包含参数的sql语句，对数据表进行新增、编辑、删除操作，实现数据同步等目的，其中参数代表当前数据
</blockquote>
<span style="position: absolute;top: 20px; right: 20px;" id="blockquote_close">
    <i class="fa fa-close" style="color: #FFAB00;" id="tipsWhereStr"></i>
</span>
<table class="layui-hide" id="tableId" lay-filter="tableFilter"></table>

<script src="~/lib/layui/layui.js" charset="utf-8"></script>
<script>
    layui.use(["table", "form", "exLayer", "exUtils"], function () {
        let table = layui.table;
        let form = layui.form;
        let exLayer = layui.exLayer;
        let exUtils = layui.exUtils;
        let $ = layui.$;

        $("#blockquote_close").click(function () {
            $(".layui-elem-quote").attr("style", "display:none");
            $("#blockquote_close").attr("style", "display:none");
        });

        let ThisTable = table.render({
            elem: "#tableId",
            url: "/SysTable/TbEvent/GetList?tbid=" + $("#tbid").val(),
            defaultToolbar: [{ title: '刷新', layEvent: 'refresh', icon: 'layui-bg-gray layui-icon-refresh' }],
            toolbar: "#toolbarTpl",
            cols: [[
                { type: "numbers", title: "NO.", fixed: 'left' },
                { field: "EventId", title: "类别", width: 150, sort: true },
                { field: "FullName", title: "名称", width: 150, sort: true },
                { field: "ExecuteSql", title: "执行Sql" },
                { field: "OrderNo", title: "序号", width: 80, sort: true },
                { title: "操作", width: 120, align: "center", fixed: layui.device().mobile ? null : 'right', templet: "#operationTpl" }
            ]],
            done: function (res, curr, count) {
                console.log(res, curr, count);
            }
        });

        table.on("toolbar(tableFilter)", function (obj) {
            switch (obj.event) {
                case "add":
                    add();
                    break;
                case "refresh":
                    refresh();
                    break;
            }
        });

        table.on("tool(tableFilter)", function (obj) {
            let data = obj.data;
            switch (obj.event) {
                case "edit":
                    edit(data.Id);
                    break;
                case "del":
                    del(data.Id);
                    break;
            }
        });

        function add() {
            exLayer.open("新建", "/SysTable/TbEvent/Add?tbid=" + $("#tbid").val(), '100%', '100%', '0px', '0px', null, null);
        }

        function edit(id, type) {
            exLayer.open("编辑", "/SysTable/TbEvent/Edit?id=" + id + "&tbid=" + $("#tbid").val(), '100%', '100%', '0px', '0px', null, null);
        }

        function del(id) {
            exLayer.confirm("确定要删除吗？", function () {
                exUtils.ajax("/SysTable/TbEvent/Delete", "get", { id: id }, true).done(function (response) {
                    exUtils.tableSuccessMsg(response.message);
                    refresh();
                }).fail(function (error) {
                    console.log(error);
                });
            })
        }

        function refresh() {
            ThisTable.reload({
                url: "/SysTable/TbEvent/GetList?tbid=" + $("#tbid").val()
            });
        }
    });
</script>

<script type="text/html" id="toolbarTpl">
    <button type="button" class="layui-btn layui-btn-normal layui-btn-sm" lay-event="add" id="add"><i class="fa fa-plus"></i>新建事件</button>
</script>

<!-- 行工具栏模板 -->
<script type="text/html" id="operationTpl">
    <a href="javascript:;" class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit" id="edit">编辑</a>
    <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del" id="del">删除</a>
</script>